package com.example.sofa_module.detail;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.ijk.widget.IjkView;
import com.example.adapter_sdk.quick.BaseQuickAdapter;
import com.example.common.mvvm.view.BaseActivity;
import com.example.loadimg_sdk.ImgLoad;
import com.example.sofa_module.R;
import com.example.sofa_module.SofaFeedEntity;
import com.example.sofa_module.databinding.ActivityDetailBinding;
import com.tb.emoji.Emoji;
import com.tb.emoji.EmojiUtil;
import com.tb.emoji.FaceFragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Route(path = "/sofa/DetailActivity")
public class DetailActivity extends BaseActivity<ActivityDetailBinding,CommentViewModel> implements FaceFragment.OnEmojiClickListener, BaseQuickAdapter.OnItemChildClickListener {
    private boolean flag;
    private List<CommentEntity.DataBean.Comment>list=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        ActivityDetailBinding binding=ActivityDetailBinding.inflate(inflater);
        return binding;
    }

    @Override
    public void initView() {

    }


    @Override
    public void initData() {
        Intent intent = getIntent();
        SofaFeedEntity.DataBean.VideoData videobean = (SofaFeedEntity.DataBean.VideoData) intent.getSerializableExtra("videobean");
        vm.requestComment(0,1,10,0);
        Integer itemType = videobean.itemType;
        switch (itemType) {
            case 0: {
                v.detailJik.setVisibility(View.GONE);
                v.detailTxt.setVisibility(View.VISIBLE);
                v.detailTxt.setText(videobean.activityText);
            }
            break;
            case 1: {
                v.detailJik.setVisibility(View.VISIBLE);
                v.detailTxt.setVisibility(View.GONE);
                v.detailJik.setImgUrl(videobean.cover);
                v.detailJik.setMediaControllerVisibility(View.GONE);
            }
            break;
            case 2: {
                v.detailTxt.setVisibility(View.GONE);
                v.detailJik.setVisibility(View.VISIBLE);
                v.detailJik.setImgUrl(videobean.cover);
                v.detailJik.setPlayUrl(videobean.url);
                v.detailJik.start();
            }
            break;
        }
        v.sendEmoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!flag) {
                    v.emojiFrame.setVisibility(View.VISIBLE);
                    FaceFragment faceFragment = FaceFragment.Instance();
                    getSupportFragmentManager().beginTransaction().add(R.id.emoji_frame,faceFragment).commit();
                    flag = false;
                } else {
                    v.emojiFrame.setVisibility(View.GONE);
                    flag = true;
                }
            }
        });
        ImgLoad.loadMatchImg(videobean.author.avatar,v.include.detailHead);
        v.include.detailName.setText(videobean.author.name);
        v.include.detailFeed.setText(videobean.feeds_text);
        vm.commentList.observe(this, new Observer<List<CommentEntity.DataBean.Comment>>() {
            @Override
            public void onChanged(List<CommentEntity.DataBean.Comment> comments) {
                list=comments;
                CommentAdapter commentAdapter = new CommentAdapter();
                LinearLayoutManager layoutManager = new LinearLayoutManager(DetailActivity.this);
                layoutManager.setOrientation(RecyclerView.VERTICAL);
                v.include.detailComment.setLayoutManager(layoutManager);
                commentAdapter.setNewInstance(comments);
                v.include.detailComment.setAdapter(commentAdapter);
                v.include.detailComment.scrollToPosition(0);
                delComment(commentAdapter);
            }
        });
        publishComment();

    }

    private void delComment(CommentAdapter commentAdapter) {
        commentAdapter.setOnItemChildClickListener(this);
    }

    private void publishComment() {
        v.detailSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = v.editText.getText().toString();
                if (TextUtils.isEmpty(s))return;
                vm.requestAdd(s,0,null,1,0,null,0);
                v.include.detailComment.scrollToPosition(0);
                v.editText.setText("");
            }
        });
    }


    @Override
    public void onEmojiDelete() {
        String text = v.editText.getText().toString();
        if (text.isEmpty()) {
            return;
        }
        if ("]".equals(text.substring(text.length() - 1, text.length()))) {
            int index = text.lastIndexOf("[");
            if (index == -1) {
                int action = KeyEvent.ACTION_DOWN;
                int code = KeyEvent.KEYCODE_DEL;
                KeyEvent event = new KeyEvent(action, code);
                v.editText.onKeyDown(KeyEvent.KEYCODE_DEL, event);
                displayTextView();
                return;
            }
            v.editText.getText().delete(index, text.length());
            displayTextView();
            return;
        }
        int action = KeyEvent.ACTION_DOWN;
        int code = KeyEvent.KEYCODE_DEL;
        KeyEvent event = new KeyEvent(action, code);
        v.editText.onKeyDown(KeyEvent.KEYCODE_DEL, event);
        displayTextView();
    }

    private void displayTextView() {
        try {
//            EmojiUtil.handlerEmojiText(textView, editText.getText().toString(), this);
            EmojiUtil.handlerEmojiText(v.editText, v.editText.getText().toString(), this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEmojiClick(Emoji emoji) {
        if (emoji != null) {
            int index = v.editText.getSelectionStart();
            Editable editable =v.editText .getEditableText();
            if (index < 0) {
                editable.append(emoji.getContent());
            } else {
                editable.insert(index, emoji.getContent());
            }
        }
        displayTextView();
    }

    @Override
    public void onItemChildClick(RecyclerView.Adapter adapter, int id, int position) {
        Long commentId = list.get(position).commentId;
        Long itemId = list.get(position).itemId;
        Long userId = list.get(position).userId;
        if (id==R.id.comment_delete){
            vm.delComment(commentId,userId,itemId);
        }
    }
}
